<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="LAY-list-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">仓库名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="warehouse_name" placeholder="请输入仓库名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">仓库代码</label>
                    <div class="layui-input-block">
                        <input type="text" name="id" placeholder="请输入仓库代码" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">所属国家</label>
                    <div class="layui-input-block">
                        <input type="text" name="country_name" placeholder="请输入所属国家" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <select name="status">
                            <option value="">全部</option>
                            <option value="-1">禁用</option>
                            <option value="1">启用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">仓库类型</label>
                    <div class="layui-input-block">
                        <select name="type">
                            <option value="">全部</option>
                            <option value="1">店仓</option>
                            <option value="2">国家仓</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button style="display:none" class="down" lay-submit="" lay-filter="LAY-list-down">下载</button>
                    <button class="layui-btn layuiadmin-btn-useradmin search" lay-submit lay-filter="LAY-list-search">搜索</button>
                    <button type="reset" class="reset layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-list"  lay-filter="LAY-list"></table>
            <!-- 头部工具栏 -->
            <script type="text/html" id="toolbar">
                <div class="layui-btn-group fl layui-btn-group-jy">
                    <a  class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-add-circle-fine" lay-event="add">&nbsp;新增</a>
                </div>
            </script>
            <!-- 行内工具 -->
            <script type="text/html" id="LAY-list-toolbar">
                {{# if(d.status == 1){ }}
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disable">禁用</a>
                {{# }else{ }}
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="enable">启用</a>
                {{# } }}
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
            </script>
        </div>
    </div>
</div>

<script>
    layui.use(['admin', 'table', 'form','toolJ','laydate'], function () {
        var shop_companys = [];
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , toolJ = layui.toolJ
            , laydate = layui.laydate
            , form = layui.form
            , user = layui.data('yujiso');
        //日期时间范围
        laydate.render({
            elem: '#date'
            ,type: 'datetime'
            ,range: true
        });
        getShopCompanys();
        //用户管理
        table.render({
            elem: '#LAY-list'
            ,url: '/index/Warehouse/getWarehouseList'
            ,toolbar: '#toolbar'
            ,cellMinWidth:80
            ,defaultToolbar: ['filter']
            ,cols: [[
                {field: 'warehouse_name', title: '仓库名称', width:165}
                ,{field: 'id', title: '仓库代码'}
                ,{field: 'address', title: '仓库地址'}
                ,{field: 'company_name', title: '所属公司', width:165}
                ,{field: 'shop_name', title: '所属店铺', width:165}
                ,{field: 'type_name', title: '仓库类型'}
                ,{field: 'status_name', title: '状态'}
                ,{field: 'operator', title: '修改人'}
                ,{field: 'update_time', title: '修改时间', width:165}
                ,{fixed: 'right', align:'center', title:'操作', toolbar: '#LAY-list-toolbar', width:150}
            ]]
            ,page: true
            ,limit: 20
            ,text: {none: '暂无相关数据'}
        });
        //监听头部工具栏 操作
        table.on('toolbar(LAY-list)', function (obj) {
            //选中的数据
            var checkStatus = table.checkStatus('LAY-list')
                , data = checkStatus.data;
            //event对应模板的
            switch (obj.event) {
                case 'export':
                    $('.down').click();
                    break;
                case 'update':
                    toolJ.ajax({"url":"/index/exchange/getExchangeByApi"});
                    break;
                case 'add':
                    admin.popup({
                        title: '新增'
                        ,area: ['420px', '500px']
                        ,id: 'LAY-popup-content-add'
                        ,success: function(layero, index){
                            view(this.id).render('Warehouse/edit').done(function(){

                                form.render(null, 'layui-warehouse-edit-form-list');
                                //监听提交
                                form.on('submit(layuiadmin-app-form-submit)', function(data){
                                    var field = data.field; //获取提交的字段
                                    field['operator'] = user.cn_name;
                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
//                                    toolJ.ajax({"data":field,"url":"/index/Warehouse/edit"});
                                    $.ajax({
                                        url: "/index/Warehouse/edit",
                                        type: "POST",
                                        data: field,
                                        dataType: "json",
                                        success: function (res) {
                                            var icon = 1;
                                            if (res.code == 1){
                                                layer.closeAll();
                                                $('.search').click();
                                                layui.table.reload('LAY-app-content-list'); //重载表格
                                                layer.close(index); //执行关闭
                                                table.reload();
                                            }else {
                                                icon = 2;
                                            }
                                            layer.msg(res.msg, {icon: icon});
                                        },
                                        error: function (res) {
                                            layer.closeAll();
                                            layer.msg("系统错误！"+res.msg, {icon: 2});
                                        }
                                    });
                                });
                            });
                        }
                    });
                    break;

            }
        });
        //监听行内操作
        table.on('tool(LAY-list)', function(obj){
            var data = obj.data;
            switch (obj.event) {
                case 'disable':
                    toolJ.ajax({"data":{"id":data.id,"status":-1},"url":"/index/Warehouse/disableOrEnable"});
                    break;
                case 'enable':
                    toolJ.ajax({"data":{"id":data.id,"status":1},"url":"/index/Warehouse/disableOrEnable"});
                    break;
                case 'edit':
                    admin.popup({
                        title: '编辑'
                        ,area: ['420px', '500px']
                        ,id: 'LAY-popup-content-add'
                        ,success: function(layero, index){
                            view(this.id).render('Warehouse/edit',data).done(function(){
                                var type = data.type;
                                var selectHtml = "";
                                var obj2 =  $('#shop_html');
                                if (type == 1){
                                    obj2.html("所属店铺");
                                    selectHtml = '<option value="'+data.shop_name+'">'+data.shop_name+'</option>';
                                }else {
                                    obj2.html("所属公司");
                                    selectHtml = '<option value="'+data.company_name+'">'+data.company_name+'</option>';
                                }
                                $("select[name='shop_name']").append(selectHtml);
                                form.render(null, 'layui-warehouse-edit-form-list');
                                //监听提交
                                form.on('submit(layuiadmin-app-form-submit)', function(data){
                                    var field = data.field; //获取提交的字段
                                    field['operator'] = user.cn_name;
                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
//                                    toolJ.ajax({"data":field,"url":"/index/Warehouse/edit"});
                                    $.ajax({
                                        url: "/index/Warehouse/edit",
                                        type: "POST",
                                        data: field,
                                        dataType: "json",
                                        success: function (res) {
                                            var icon = 1;
                                            if (res.code == 1){
                                                layer.closeAll();
                                                $('.search').click();
                                                layui.table.reload('LAY-app-content-list'); //重载表格
                                                layer.close(index); //执行关闭
                                                table.reload();
                                            }else {
                                                icon = 2;
                                            }
                                            layer.msg(res.msg, {icon: icon});
                                        },
                                        error: function (res) {
//                                            layer.closeAll();
                                            layer.msg("系统错误！"+res.msg, {icon: 2});
                                        }
                                    });
                                });
                            });
                        }
                    });
                    break;

            }
        });
        /*监听下拉框 仓库类型值改变*/
        form.on('select(warehouse_type)', function(data){
            var obj = $("select[name='shop_name']");
            var obj2 =  $('#shop_html');
            obj.empty();
            var type = data.value;
            /*动态给下拉框赋值*/
            var selectHtmlShop = '<option value="">请选择</option>';
            var selectHtmlCompany = '<option value="">请选择</option>';
            if (type == 1){
                obj2.html("所属店铺");
                var shops = shop_companys.shops;
                for (var i in shops) {
                    selectHtmlShop += '<option value="' +  shops[i] + '">' + shops[i] + '</option>';
                }
                obj.append(selectHtmlShop);
            }else if(type == 2){
                obj2.html("所属公司");
                var companys = shop_companys.companys;
                for (var i_ in companys) {
                    selectHtmlCompany += '<option value="' + companys[i_] + '">' + companys[i_] + '</option>';
                }
                obj.append(selectHtmlCompany);
            }
            form.render(null, 'layui-warehouse-edit-form-list');
        });
        function getShopCompanys() {
            $.ajax({
                url: "/index/Warehouse/getShopCompanys",
                type: "POST",
                data: [],
                dataType: "json",
                success: function (res) {
                    shop_companys = res;
                },
                error: function (res) {
                    layer.msg("系统错误！"+res.msg, {icon: 2});
                }
            });

        }
        /*重新渲染界面*/
        form.render(null, 'LAY-list-form');
        /*重置数据，刷新列表*/
        $('.reset').on('click', function () {
            $("input").val('');
            $("textarea").val('');
            $(".selector").val('');
            table.reload('LAY-list', {where: ''});
        });
    });
</script>